package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.result.PageResult;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {
    /**
     * insert orders
     * @param orders
     */
    void insert(Orders orders);



    /**
     * 根据订单号查询订单
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);


    @Update("update orders set status = #{orderStatus},pay_status = #{orderPaidStatus} ,checkout_time = #{check_out_time} " +
            "where number = #{orderNumber}")
    void updateStatus(Integer orderStatus, Integer orderPaidStatus, LocalDateTime check_out_time, String orderNumber);

    /**
     * 分页查询
     * @param orderPageQueryDTO
     * @return
     */
    Page<Orders> list(OrdersPageQueryDTO orderPageQueryDTO);

    /**
     * 修改订单状态
     * @param id
     */
   @Update("update orders set status = 3 where id = #{id}")
    void setStatus(Long id);

    /**
     * 根据订单id查询订单对象
     * @param id
     * @return
     */
    @Select("select * from orders where id = #{id}")
    Orders ordersById(Long id);

    /**
     * 统计各个订单状态
     * @return
     */
    @Select("SELECT " +
            "SUM(CASE WHEN status = 2 THEN 1 ELSE 0 END) AS  toBeConfirmed, " +
            "SUM(CASE WHEN status = 3 THEN 1 ELSE 0 END) AS confirmed, " +
            "SUM(CASE WHEN status = 4 THEN 1 ELSE 0 END) AS deliveryInProgress " +
            "FROM orders")
    OrderStatisticsVO getTotalStatus();
    @Select(("select * from orders where id = #{id}"))
    Orders getById(Long id);

    @Select("select * from orders where status = #{status} and order_time < #{localDateTime}")
    List<Orders> getByStatusAndOrderTimeLT(Integer status, LocalDateTime localDateTime);

    /**
     * 统计订单数量
     * @param map
     * @return
     */
    Integer countOrders(Map map);

    List<GoodsSalesDTO> getSalesTop(LocalDateTime begin , LocalDateTime end);
}
